<?php 
namespace Base\View\Helper;

use Zend\View\Helper\AbstractHelper;

class FileUpload extends AbstractHelper {
	
        /**
         * @method __invoke()
         * instancia objeto FileUpload
         * @param $qtdImages = quantidade de imagens que poderá ser inserido
         * @param $maxWidth = largura máxima
         * @param $maxHeight = altura máxima
         * @param $entity = nome da pasta será o nome da entidade
         * $param $id = id do produto, noticia, evento, loja, etc...
         */
        public function __invoke($qtdImages, $maxWidth, $maxHeight, $entity, $id, $maxWidthThumbnail = 175, $maxHeightThumbnail = 175, $idPlugin = NULL) {        	
        	        	         
        	return ('<div class="container">
				    <!-- The file upload form used as target for the file upload widget -->
				    <form id="fileupload'. $idPlugin .'" action="" method="POST" enctype="multipart/form-data" data-upload-template-id="template-upload-'.  $idPlugin.'" data-download-template-id="template-download-'.  $idPlugin.'">
				        <!-- Redirect browsers with JavaScript disabled to the origin page -->
				        <noscript><input type="hidden" name="redirect" value="http://blueimp.github.com/jQuery-File-Upload/"></noscript>
				        <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
				        <div class="row fileupload-buttonbar">
				            <div class="span7">
				                <!-- The fileinput-button span is used to style the file input field as button -->
				                <span class="btn fileinput-button">
				                    <i class="icon-plus"></i>
				                    <span>Adicionar arquivos...</span>
				                    <input type="file" name="files[]" multiple>
				                </span>
				                <button type="reset" class="btn cancel">
				                    <i class="icon-ban-circle"></i>
				                    <span>Cancelar upload</span>
				                </button>
				                <button type="button" class="btn delete">
				                    <i class="icon-trash"></i>
				                    <span>Deletar</span>
				                </button>
				                <input type="checkbox" class="toggle">
				            </div>
				            <!-- The global progress information -->
				            <div class="span5 fileupload-progress fade">
				                <!-- The global progress bar -->
				                <div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
				                    <div class="bar" style="width:0%;"></div>
				                </div>
				                <!-- The extended global progress information -->
				                <div class="progress-extended">&nbsp;</div>
				            </div>
				        </div>
				        <!-- The loading indicator is shown during file processing -->
				        <div class="fileupload-loading"></div>
				        <br>
				        <!-- The table listing the files available for upload/download -->
				        <table role="presentation" class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>
				    </form>
				</div>
				<div id="modal-gallery" class="modal modal-gallery hide fade" data-filter=":odd" tabindex="-1">
				  
			        <div class="modal-header">
				        <a class="close" data-dismiss="modal">&times;</a>
				        <h3 class="modal-title"></h3>
				    </div>
				    <div class="modal-body"><div class="modal-image"></div></div>
				    <div class="modal-footer">
				        <a class="btn modal-download" target="_blank">
				            <i class="icon-download"></i>
				            <span>Download</span>
				        </a>
				        <a class="btn btn-success modal-play modal-slideshow" data-slideshow="5000">
				            <i class="icon-play icon-white"></i>
				            <span>Apresentação de slides</span>
				        </a>
				        <a class="btn btn-info modal-prev">
				            <i class="icon-arrow-left icon-white"></i>
				            <span>Anterior</span>
				        </a>
				        <a class="btn btn-primary modal-next">
				            <span>Próxima</span>
				            <i class="icon-arrow-right icon-white"></i>
				        </a>
				    </div>
				</div>
				<!-- The template to display files available for upload -->
				<script id="template-upload-'.  $idPlugin.'" type="text/x-tmpl">
				{% for (var i=0, file; file=o.files[i]; i++) { %}
			    <tr class="template-upload fade">
			        <td class="preview"><span class="fade"></span></td>
			        <td class="name"><span>{%=file.name%}</span></td>
			        <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
			        {% if (file.error) { %}
			            <td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td>
			        {% } else if (o.files.valid && !i) { %}
			            <td>
			                <div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>
			            </td>
			            <td>{% if (!o.options.autoUpload) { %}
			                <button class="btn btn-primary start">
			                    <i class="icon-upload icon-white"></i>
			                    <span>Start</span>
			                </button>
			            {% } %}</td>
			        {% } else { %}
			            <td colspan="2"></td>
			        {% } %}
			        <td>{% if (!i) { %}
			            <button class="btn btn-warning cancel">
			                <i class="icon-ban-circle icon-white"></i>
			                <span>Cancel</span>
			            </button>
			        {% } %}</td>
			    </tr>
			{% } %}
				</script>
				<!-- The template to display files available for download -->
				<script id="template-download-'.  $idPlugin.'" type="text/x-tmpl">
			{% for (var i=0, file; file=o.files[i]; i++) { %}
			    <tr class="template-download fade">
			        {% if (file.error) { %}
			            <td></td>
			            <td class="name"><span>{%=file.name%}</span></td>
			            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
			            <td class="error" colspan="2"><span class="label label-important">Erro</span> {%=file.error%}</td>
			        {% } else { %}
			            <td class="preview">{% if (file.thumbnail_url) { %}
			                <a href="{%=file.url%}" title="{%=file.name%}" data-gallery="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
			            {% } %}</td>
			            <td class="name">
			                <a href="{%=file.url%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&\'gallery\'%}" download="{%=file.name%}">{%=file.name%}</a>
			            </td>
			            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
			            <td colspan="2"></td>
			        {% } %}
			        <td>
			            <button class="btn delete" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"{% if (file.delete_with_credentials) { %} data-xhr-fields=\'{"withCredentials":true}\'{% } %}>
			                <i class="icon-trash"></i>
			                <span>Deletar</span>
			            </button>
			            <input type="checkbox" name="delete" value="1" class="toggle">
			        </td>
			    </tr>
			{% } %}
				</script>
				<script type="text/javascript">
				$(function () {
				    \'use strict\';
			        
				    $("#fileupload'.  $idPlugin.'").fileupload({
						autoUpload: true,
						maxNumberOfFiles: '. $qtdImages .',
						url: "/uploads/index.php?entity=' . $entity .'&id=' . $id . '&maxWidthThumbnail=' . $maxWidthThumbnail . '&maxHeightThumbnail=' . $maxHeightThumbnail . '",
						maxFileSize: 5000000,
						acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
						process: [
							{
								action: "load",
								fileTypes: /^image\/(gif|jpeg|png)$/,
								maxFileSize: 20000000 // 20MB
							},
							{
								action: "resize",
								maxWidth: '. $maxWidth .',
								maxHeight: '. $maxHeight .'
							},
							{
								action: "save"
							}
						]
				    });
			        
					// Load existing files:
					$("#fileupload'.  $idPlugin.'").addClass("fileupload-processing");
					$.ajax({
						// Uncomment the following to send cross-domain cookies:
						//xhrFields: {withCredentials: true},
						url: $("#fileupload'.  $idPlugin.'").fileupload("option", "url"),
						dataType: "json",
						context: $("#fileupload'.  $idPlugin.'")[0]
					}).always(function (result) {
						$(this).removeClass("fileupload-processing");
					}).done(function (result) {
						$(this).fileupload("option", "done")
							.call(this, null, {result: result});
					});
			        
				});
				</script>
				<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE8+ -->
				<!--[if gte IE 8]><script src="js/cors/jquery.xdr-transport.js"></script><![endif]-->');
        }
}